
/*##########################################################################################################################################################################################
41    B  Get the list of shared folders in the avatar's inventory : @getinv[:folder1/.../folderN]=<channel_number>     	
41  U B  Get the list of shared folders in the avatar's inventory, with information about worn items : @getinvworn[:folder1/.../folderN]=<channel_number>     	
    	*  0 : No item is present in that folder
    	* 1 : Some items are present in that folder, but none of them is worn
    	* 2 : Some items are present in that folder, and some of them are worn
    	* 3 : Some items are present in that folder, and all of them are worn 
41  U B  Get the path to a shared folder by giving a search criterion : 
		@findfolder:part1[&&...&&partN]=<channel_number>     	
41    B  Force attach items contained inside a shared folder : 
		@attach:<folder1/.../folderN>=force (*)     	
41  U B  Force attach items contained inside a shared folder, and its children recursively : 
		@attachall:<folder1/.../folderN>=force (*)     	
41    B  Force detach items contained inside a shared folder : @detach:<folder_name>=force (*)     	
41  U B  Force detach items contained inside a shared folder, and its children recursively : 
		@detachall:<folder1/.../folderN>=force (*)     	
41    B  Get the path to the shared folder containing a particular object/clothing : @getpath[:<attachpt> or 
		<clothing_layer>]=<channel_number> 
41    B  Force attach items contained into a shared folder that contains a particular object/clothing : 
		@attachthis[:<attachpt> or <clothing_layer>]=force (*)     
41    B  Force attach items contained into a shared folder that contains a particular object/clothing, 
		and its children folders : @attachallthis[:<attachpt> or <clothing_layer>]=force (*) 
41    B  Force detach items contained into a shared folder that contains a particular object/clothing : 
		@detachthis[:<attachpt> or <clothing_layer>]=force (*)     
41    B  Force detach items contained into a shared folder that contains a particular object/clothing, 
		and its children folders : @detachallthis[:<attachpt> or <clothing_layer>]=force (*) 
##########################################################################################################################################################################################*/	

list gParse;
integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
list gMenu=[];
list statesMenu=[];
list gAvatarAllow;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
key ToucherID;
integer channel_dialog;
integer listen_id;
integer listen_id2;
integer channel_dialog2=13337;
string msg ="Shared Folder";
integer gPage;
list gPlace=[];
list folderInfo=[];
integer steps;
string TICKED = "(*)";  //checked
string STICKED = "(.)";   //partchecked
string UNTICKED = "( )";
string FOLDER = " / ";
list shortfolders = [];
list longfolders = [];
string ATTACH_ALL = "(+) *All*";
string DETACH_ALL = "(-) *All*";
string ATTACH_THIS = "(+) *This*";
string DETACH_THIS = "(-) *This*";
list pageMenu(list in ,integer page)
{
	list out;
	integer maxpages =llGetListLength(in)/9;
	page = (page)%(maxpages+1);
	out =["<<","[Back]",">>"];
	integer _listcount =(page *9)+9;
	integer _i=_listcount-9;
	
	for (_i;_i<_listcount;_i++)
	{
		if(_i<=llGetListLength(in)&&llList2String(in,_i)!="")
		out+=llList2String(in,_i);
	}
	return out;
}

llRlvSay(string command)
{
	if(attachment==TRUE)
	{
		llOwnerSay("@"+command);
	}
	else
	{
		llSay(relaychannel,cmdname + "," + (string)victim  + "," + "@"+command);
	}
}

MakeRLVState()
{
	if(attachment==TRUE)
	{

	
	}
}

makeMenu()
{
	gMenu=[" "];
	llRlvSay("getinvworn="+(string)channel_dialog2);
	listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
	llSetTimerEvent(45.0);
}

displayMenu()
{
	
	list _menu;
	if(llGetListLength(gMenu)<10)
	{
		_menu= [" ","[Back]", " "]+gMenu;

	}
	else if(llGetListLength(gMenu)>=10)
	{
		_menu= pageMenu(gMenu,gPage);
	}
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,_menu, channel_dialog);
	llSetTimerEvent(45.0);
}

default {
    state_entry() 
    {
    	    
    }
    
    listen(integer channel, string name, key id, string message)
    {
    	if(channel == channel_dialog2)
    	{
    		longfolders =[];
	        shortfolders =[];
    		list data = llParseStringKeepNulls(message, [","], []);
    		string first = llList2String(data, 0);
    		data = llListSort(llList2List(data, 1, -1), 1, 1);
    		integer i; list item; integer worn;
	        for (i=0;i<llGetListLength(data);i++) 
	        {
	            item=llParseString2List(llList2String(data,i),["|"],[]);
	            string folder = llList2String(item,0);
	            worn=llList2Integer(item,1);
	            if  (worn%10>=1)
	            {
	                longfolders += folder;
	                shortfolders += [llGetSubString(FOLDER+folder,0,20)];
	                msg+=folder+"\n";
	            }
	            else if  (worn==10)
	            {
	                longfolders += folder;
	                shortfolders += [llGetSubString(UNTICKED+folder,0,20)];
	                msg+=folder+"\n";
	            }
	            else if  (worn==20)
	            {
	                longfolders += folder;
	                shortfolders += [llGetSubString(STICKED+folder,0,20)];
	                msg+=folder+"\n";
	            }
	            else if  (worn==30)
	            {
	                longfolders += folder;
	                shortfolders += [llGetSubString(TICKED+folder,0,20)];
	                msg+=folder+"\n";
	            }
	            
	        }
	        item=llParseString2List(first,["|"],[]);
            worn=llList2Integer(item,0);
            if  (worn%10==1)  {shortfolders+= [ATTACH_ALL]; }
            else if  (worn%10==2)  {shortfolders+= [ATTACH_ALL, DETACH_ALL];}
            else if  (worn%10==3)  {shortfolders+= [DETACH_ALL]; }
            if  (worn/10==1)  {shortfolders+= [ATTACH_THIS];}
            else if  (worn/10==2)  {shortfolders+= [ATTACH_THIS, DETACH_THIS];}
             else if  (worn/10==3)  {shortfolders+= [DETACH_THIS];}
	        gMenu=shortfolders;
    		llListenRemove(listen_id2);
    		displayMenu();
    	}
    	if(channel == channel_dialog)
    	{
    		llListenRemove(listen_id);
    		if(message=="<<")
	  		{
	  			gPage--;
				integer maxpages =llGetListLength(gMenu)/9;
				if(gPage<0)
				{
					gPage = maxpages;
				}
				displayMenu();
	  		}
	  		else if(message==">>")
	  		{
	  			gPage++;
	  			displayMenu();
	  		}
	  		else if(message=="Attach")
	  		{
	  			string _place;
	    		integer _j=0;
	    		_place+=llList2String(gPlace,_j);
	    		_j++;
	    		for (_j;_j<steps;_j++)
	    		{
	    			_place+="/"+llList2String(gPlace,_j);
	    		}
	    			
	    		llRlvSay("attachall:"+_place+"=force");
	    		llRlvSay("getinvworn:"+_place+"="+(string)channel_dialog2);
	    		listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
				llSetTimerEvent(45.0);
	  		}
	  		else if(message=="Detach")
	  		{
	  			string _place;
	    		integer _j=0;
	    		_place+=llList2String(gPlace,_j);
	    		_j++;
	    		for (_j;_j<steps;_j++)
	    		{
	    			_place+="/"+llList2String(gPlace,_j);
	    		}
	    		llRlvSay("detachall:"+_place+"=force");
	    		llRlvSay("getinvworn:"+_place+"="+(string)channel_dialog2);
	    		listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
				llSetTimerEvent(45.0);
	  		}
	  		else if(message==ATTACH_ALL)
	  		{
	  			string _place;
	    		integer _j=0;
	    		for (_j;_j<steps;_j++)
	    		{
	    			_place+=llList2String(gPlace,_j)+"/";
	    		}
	    		llRlvSay("attachall:"+_place+"=force");
	    		llRlvSay("getinvworn:"+_place+"="+(string)channel_dialog2);
	    		listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
				llSetTimerEvent(45.0);
	  		}
	  		else if(message==DETACH_ALL)
	  		{
	  			string _place;
	    		integer _j=0;
	    		for (_j;_j<steps;_j++)
	    		{
	    			_place+=llList2String(gPlace,_j)+"/";
	    		}
	    		llRlvSay("detachall:"+_place+"=force");
	    		llRlvSay("getinvworn:"+_place+"="+(string)channel_dialog2);
	    		listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
				llSetTimerEvent(45.0);
	  		}
	  		else if(message==ATTACH_THIS)
	  		{
	  			string _place;
	    		integer _j=0;
	    		for (_j;_j<steps;_j++)
	    		{
	    			_place+=llList2String(gPlace,_j)+"/";
	    		}
	    		llRlvSay("attach:"+_place+"=force");
	    		llRlvSay("getinvworn:"+_place+"="+(string)channel_dialog2);
	    		listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
				llSetTimerEvent(45.0);
	  		}
	  		else if(message==DETACH_THIS)
	  		{
	  			string _place;
	    		integer _j=0;
	    		for (_j;_j<steps;_j++)
	    		{
	    			_place+=llList2String(gPlace,_j)+"/";
	    		}
	    		llRlvSay("detach:"+_place+"=force");
	    		llRlvSay("getinvworn:"+_place+"="+(string)channel_dialog2);
	    		listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
				llSetTimerEvent(45.0);
	  		}
	  		else if(message=="[Back]")
	  		{
	    		gPlace=llDeleteSubList(gPlace, steps-1, steps-1);
	    		
	    		if(steps>0)
	    		{
	    			steps--;
		  			string _place;
		    		integer _j=0;
		    		for (_j;_j<steps;_j++)
		    		{
		    			_place+=llList2String(gPlace,_j)+"/";
		    		}
		    		msg = "Path:#RLV/"+_place+"\n";
		    		llRlvSay("getinvworn:"+_place+"="+(string)channel_dialog2);
					listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
					llSetTimerEvent(45.0);
	    		}
	    		else
	    		{
	    			llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_RLV", ToucherID);	
	    		}
	  		}
	  		else
	  		{
	  			string _stripped;
	  			integer _i = llListFindList(gMenu, [message]);
	  			string preComand=llGetSubString(message,0,2);
	  			gPlace+=[llList2String(longfolders,_i)];
		    	steps++;
		    	string _place;
		    	integer _j=0;
		    	for (_j;_j<steps;_j++)
		    	{
		    		_place+=llList2String(gPlace,_j)+"/";
		    		
		    	}
		    	msg = "Path:#RLV/"+_place+"\n";
		    	if(preComand==FOLDER)
	  			{
		    		llRlvSay("getinvworn:"+_place+"="+(string)channel_dialog2);
					listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
					llSetTimerEvent(45.0);
	  			}
	  			else
	  			{
	  				gMenu=[];
	  				if(preComand==TICKED){gMenu+="Detach";displayMenu();}
	  				else if(preComand==STICKED){gMenu+="Attach";gMenu+="Detach";displayMenu();}
	  				else if(preComand==UNTICKED){gMenu+="Attach";displayMenu();}
	  			}
	
	  		}
    	}
    	
    }
    
    on_rez(integer on)
    {
    
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				gPlace=[];
  				steps=0;
  				makeMenu();
  				msg = "Path:#RLV/\n";
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str =="attachment")attachment=TRUE;
  			else if(str =="device")attachment=FALSE;
  			else if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
				llListenRemove(listen_id2);
  			}
  		}
  		if(num==gPlugin)
  		{
  			if(str=="ON_REZ")
  			{
  				MakeRLVState();
  			}
  			if(str=="RESET")
  			{
  				
  			}
  		}
  		if(num==gVic)
  		{
  				victim=(key)str;
  				cmdname = (string)llGetKey();
  		}
  	}
  	
  	timer()
    {
    	llSetTimerEvent(0.0);
    	llListenRemove(listen_id);
    	llListenRemove(listen_id2);
    }
}
